/*
Copyright (c) 2007, EffectiveUI, Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the EffectiveUI nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@ignore
*/
package com.effectiveui.frameworks.commando {
	
	import flash.utils.Dictionary;
	
	/**
	 * <p>
	 * Options contains a collection of Option. Options
	 * makes sure that added options are only defined once,
	 * and keeps track of required options.
	 * </p>
	 * 
	 * <p>
	 * Options is also responsible for validating parsed options for 
	 * correct arguments, required options, and possibly failing 
	 * if an unrecognized option is found.
	 * </p>
	 * 
	 * <p>
	 * Options are defined and registered with a Command in the 
	 * application's instance(s) of CommandoController. When a command line 
	 * is parsed and matched with defined Options, a copy of the Options
	 * are made and the values from any options with arguments are set 
	 * on the Option and sent along with the CommandoEvent for a Command
	 * to look up and act according to the presence or absence of options
	 * and values to options that take arguments.
	 * </p>
	 * 
	 * 
	 * @see com.effectiveui.frameworks.commando.Option
	 */		
	public class Options {
		
		private var rawOptions:Array = [];
		private var optionMap:Dictionary = new Dictionary();
		private var requiredOptions:Array = [];
		
		
		/**
		 * Add an option to the set of options. Will throw
		 * an error if an option is already defined by either its
		 * short or long option string.
		 * 
		 * @param option Option to add.
		 * @return this instance for use in chaining adds.
		 * 
		 */		
		public function addOption(option:Option):Options {
			var opt:String = option.opt;
			var longOpt:String = option.longOpt;
			var required:Boolean = option.required;
			if (optionMap[opt] != null) {
				throw new Error("option " + opt + " is already defined.");
			}
			if (optionMap[longOpt] != null) {
				throw new Error("option " + longOpt + " is already defined.");
			}
			
			// we already checked the map, so we should be safe to just add this thing to the array
			rawOptions.push(option);
			optionMap[opt] = option;
			if (required) requiredOptions[opt] = option;
			
			if (option.hasLongOpt()) {
				optionMap[longOpt] = option;
				if (required) requiredOptions[longOpt] = option;
			}
			return this;
		}
		
		/**
		 * Gets an Option for a long or short option name.
		 * 
		 * @param longOrShortOpt the long or short option name
		 * @return Option.
		 * 
		 */		
		public function getOption(longOrShortOpt:String):Option {
			return optionMap[longOrShortOpt];
		}
		
		/**
		 * Is an Option registered for a long or short option name?
		 * 
		 * @param longOrShortOpt the long or short option name
		 * @return Option.
		 * 
		 */		
		public function hasOption(longOrShortOpt:String):Boolean {
			return (getOption(longOrShortOpt) != null)
		}
		
		/**
		 * Is an Option required for a long or short option name?
		 * 
		 * @param longOrShortOpt
		 * @return Boolean
		 * 
		 */		
		public function isRequired(longOrShortOpt:String):Boolean {
			return (requiredOptions[longOrShortOpt] != null)
		}
		
		/**
		 * Are any defined options required?
		 * 
		 * @return Boolean.
		 * 
		 */		
		public function hasRequiredOptions():Boolean {
			return (requiredOptions.length > 0);
		}
		
		/**
		 * Given parsed options from a command line, add 
		 * the values of any arguments to options to those
		 * options.
		 * 
		 * @param parsedOptions Dictionary of option names and values.
		 * 
		 */		
		public function addValues(parsedOptions:Dictionary):void {
			var option:Option;
			for (var parsedOpt:String in parsedOptions) {
				option = getOption(parsedOpt);
				if (option) {
					if (option.expectsArgument) {
						option.value = parsedOptions[parsedOpt];
					} else {
						option.value = "1";
					}
				}
			}
		}
		
		/**
		 * Does the option for the long or short option name 
		 * have a value?
		 * 
		 * @param opt long or short option name
		 * @return Boolean
		 * 
		 */		
		public function hasValue(opt:String):Boolean {
			var value:String = Option(optionMap[opt]).value;
			if (value != null) return true;
			return false;
		}
		
		/**
		 * Return parsed value for option.
		 * 
		 * @param opt long or short option name
		 * @return parsed value
		 * 
		 */		
		public function getValue(opt:String):String {
			return Option(optionMap[opt]).value;
		}
		
		/**
		 * Validates parsed options. Checks if option is required and not present,
		 * if an option expects an argument and doesn't have one, and if an unrecognized
		 * option is found.
		 * 
		 * @param parsedOptions Dictionary of parsed options and values.
		 * @param failOnUnknownOption if an unknown option is found, should validation fail?
		 * @return Boolean valid?
		 * 
		 */		
		public function validate(parsedOptions:Dictionary, failOnUnknownOption:Boolean=true):Boolean {
			var valid:Boolean = true;
			// check for required args
			if (parsedOptions == null && hasRequiredOptions()) {
				return false;
			}
			
			for each (var requiredOption:Option in requiredOptions) {
				if (parsedOptions[requiredOption.opt] == null && parsedOptions[requiredOption.longOpt] == null) return false;
			}
			
			for (var parsedOpt:String in parsedOptions) {
				var option:Option;
				if (optionMap[parsedOpt] != null) {
					option = Option(optionMap[parsedOpt]);
					if (option.expectsArgument && parsedOptions[parsedOpt] == null) {
						return false;
					}
				} else if (failOnUnknownOption) {
					return false;
				}
			}
			
			return valid;
		}
		
		/**
		 * Commmando-safe clone of Options. This is mostly used
		 * to clone the Options from a command definition to allow
		 * storage of parsed command options for shipping with a 
		 * CommandoEvent.
		 * 
		 * @return cloned Options
		 * 
		 */		
		public function clone():Options {
			var clone:Options = new Options();
			for each (var option:Option in rawOptions) {
				clone.addOption(option.clone());
			}
			
			return clone;
		}
		
	}
}